package com.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.pojo.Blog;
import com.blog.pojo.ThumbsUp;
import com.blog.pojo.Views;
import com.blog.service.BlogService;
import com.blog.service.ThumbsUpService;
import com.blog.service.ViewsService;
import com.blog.utils.IpUtils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Copyright (C), 2022
 * Author: 苏大大
 * Date: 2022-04-21 16:45
 * FileName: BolgController
 * Description: 博客控制层
 */
@RestController
@RequestMapping("blog")
@Slf4j
@CrossOrigin
public class BlogController {
    @Resource
    private BlogService blogService;
    @Resource
    private ViewsService viewsService;
    @Resource
    private ThumbsUpService thumbsUpService;
    @Resource
    private RestTemplate restTemplate;  //提供多种便捷访问远程http服务的方法,简单的Restful服务模板


    private static final String REST_URL_PREFIX = "http://SERVICE-USER";

    /***
     * 博客分类分页-模糊查询
     * @param pageNumber
     * @param pageSize
     * @param title
     * @param type_id
     * @return
     */
    @GetMapping("/queryAll/type/{pageNumber}/{pageSize}")
    public  String QueryByTypeAll(
            @Param("开始页数") @PathVariable("pageNumber") Integer pageNumber,
            @Param("总页数") @PathVariable("pageSize") Integer pageSize,
            @Param("标题") @RequestParam(defaultValue = "null") @PathVariable("title") String title,
            @Param("类型") @RequestParam(defaultValue = "0") @PathVariable("type_id") Integer type_id) {

        JSONObject object =new JSONObject();
        PageInfo<Blog> PageInfo = blogService.queryByTypePageAll(pageNumber,pageSize,type_id,title);
        object.put("blogTypeList",PageInfo.getList());
        object.put("pages",PageInfo.getPages());
        object.put("blogCount",PageInfo.getTotal());
        return object.toJSONString();
        };

    /***
     * 博客标签分页
     * @param pageNumber
     * @param pageSize
     * @param tag_id
     * @return
     */
    @GetMapping("/queryAll/tag/{pageNumber}/{pageSize}")
    public  String QueryByTagAll(
            @Param("开始页数") @PathVariable("pageNumber") Integer pageNumber,
            @Param("总页数") @PathVariable("pageSize") Integer pageSize,
            @Param("标签") @RequestParam(defaultValue = "0") @PathVariable("tag_id") Integer tag_id) {

        JSONObject object =new JSONObject();
        PageInfo<Blog> PageInfo = blogService.queryByTagPageAll(pageNumber,pageSize,tag_id);
        object.put("blogTagList",PageInfo.getList());
        object.put("pages",PageInfo.getPages());
        object.put("blogCount",PageInfo.getTotal());
        return object.toJSONString();
    };

    /***
     * 博客推荐列表
     * @return
     */
    @GetMapping("queryAll/Recommend")
    public String queryRecommendBlog(){
        JSONObject object=new JSONObject();
        object.put("RecommendBlogList",blogService.queryRecommendBlog());
        return  object.toJSONString();
    };

    /**
     * 博客游览
     * @param blogId
     * @param request
     * @return
     */
    @GetMapping("/{blogId}")
    public String queryById(@Param("博客ID") @PathVariable("blogId")Long blogId, HttpServletRequest request) {
        JSONObject object=new JSONObject();
        object.put("Blog",blogService.queryByIdBlog(blogId));
        Views views=new Views();
        String ip= IpUtils.getIpAddr(request);
        views.setBlogId(blogId).setIp(ip);
        viewsService.addViews(views);
        return object.toJSONString();
    }

    /***
     * 查询用户点赞状态
     * @param blogId
     * @param session
     * @return
     */
    @GetMapping("/thumbs_up/{blogId}")
    public String selectThumbsUp(@Param("博客ID") @PathVariable("blogId")Long blogId,HttpSession session){
        JSONObject object=new JSONObject();
        String id=session.getAttribute("uid").toString();
        Long uid=Long.parseLong(id);
        if(uid!=null&& uid!=0){
            ThumbsUp thumbsUp=thumbsUpService.selectThumbsUp(blogId,uid);
            if(thumbsUp!=null){
                object.put("success",true);
                object.put("Message","用户已点赞!");
            }else{
                object.put("success",false);
                object.put("Message","用户未点赞!");
            }
            return object.toJSONString();
        }
        object.put("success",false);
        return object.toJSONString();
    }

    /***
     *
     * 用户点赞和取消点赞
     * @param blogId
     * @param session
     * @return
     */
    @PostMapping("/thumbs_up/{blogId}")
    public String updateThumbsUp(@Param("博客ID") @PathVariable("blogId")Long blogId, HttpSession session){
        JSONObject object=new JSONObject();
        Long uid=Long.parseLong(session.getAttribute("uid").toString());
        if(uid!=null&& uid!=0){
            ThumbsUp thumbsUp=thumbsUpService.selectThumbsUp(blogId,uid);
            Blog blog=blogService.queryByIdBlog(blogId);
            if(thumbsUp!=null){
              //删除点赞表对应数据
              int stater =  thumbsUpService.deleteThumbsUp(blogId,uid);
              object.put("success",true);
              object.put("Message","已取消点赞!");
              return object.toJSONString();
            }else{
                //修改博客点赞数量
                ThumbsUp thumbsup=new ThumbsUp();
                thumbsup.setBlogId(blogId).setUid(uid);
                int stater= thumbsUpService.insetThumbsUp(thumbsup);
                object.put("Message","已点赞!");
                object.put("success",true);
            }
        }
        object.put("success",false);
        return object.toJSONString();
    }

    /***
     * 测试RedisHttpSession共享
     * @param session
     * @return
     */
    @GetMapping("/getuid")
    public String getuid(HttpSession session){
        return session.getAttribute("uid").toString();
    }


    @GetMapping("blogs")
    public String queryByuIdBlog(HttpSession session){
        JSONObject object=new JSONObject();
        Long uid=Long.parseLong(session.getAttribute("uid").toString());
        if(uid!=null){
            List<Blog> blogs=blogService.queryByuIdBlog(uid);
            log.info("查询到用户所发布的相关博客信息数量为:{}",blogs.size());
            object.put("blogs",blogs);
            return  object.toJSONString();
        }else{
            object.put("error","用户未登录");
            return  object.toJSONString();
        }

    }

}
